﻿/****************
 * Copyright 2016 Brian Tabone
 * Licensed under the Apache 2.0 License
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * ConversationController
 * The controller for the REST API endpoint for conversations
 * 8/13/2016
 */

using System;
using System.Collections.Generic;
using System.Net;
using System.Web.Http;
using IAO.Common.Network.MessageImplementations;

namespace IAO.Common.Network.MessageControllers
{
    public class ConversationsController : ApiController
    {
        // GET api/conversations 
        public IEnumerable<Guid> Get()
        {
            var conversationGuids = new List<Guid>();

            foreach (var conv in Conversation.Conversations)
            {
                conversationGuids.Add(conv.ConversationID);
            }

            return conversationGuids;
        }

        // GET api/conversations/5 
        public Conversation Get(Guid id)
        {
            var conversations = Conversation.Conversations;

            return conversations.Find(cv => cv.ConversationID == id);
        }

        // Put api/conversations 
        public void Put([FromBody] string value)
        {
            throw new HttpResponseException(HttpStatusCode.NotImplemented);
        }

        // Add a message to a conversation (if the conversation doesn't exist yet, create it
        public Conversation Post([FromBody] ConversationMessage msg)
        {
            var newConvo = Conversation.Conversations.Find(cv => cv.ConversationID == msg.ConversationID);

            if (newConvo == null)
            {
                newConvo = new Conversation(msg);
            }

            return newConvo;
        }

        // DELETE api/conversations/5 
        public void Delete(Guid id)
        {
        }
    }
}